<?php
class DebugClass {

  private $debug = array();


  private function __construct() {
  }

  public static function &getInstance() {
    static $singleton = null;

    if (!is_object($singleton)) {
      $singleton = new DebugClass(false);
    }

    return $singleton;
  }

  public static function &getDebug() {
    return DebugClass::getInstance();
  }

  public static function debug($msg) {
    DebugClass::getDebug()->debug[] = $msg;
  }

  public static function toString() {
    echo DebugClass::getDebug();
  }

  public function __toString()
  {
    $output = '';

    if (count($this->debug) > 0)
    {
      $debugs =& $this->debug;

      $output = '<br><hr><br>';

      foreach ($debugs as $k => $debug) {

        $output .=  ($k+1) . ') ' .$debug . "<br><br>";
      }

      $output .= '<br><hr><br>';
    }

    return $output;
  }


}

/**
 *
 * Richiama in modo trasparante la classe Debug aggiungendo il messaggio
 * passato in input
 *
 * <code>
 *  debug ('Messaggio da scrivere');
 *
 *  debug ('Messaggio', __FUNCTION__, __FILE__, __LINE__ );
 * </code>
 *
 * @param $msg messaggio da aggiungere al debug
 */
function debug($msg, $function=null, $file=null, $line=null) {

  if ( $function != null)
  $msg .=  ' - [' . $function . ']';

  if ( $file != null )
  $msg .=  ' - [' . $file . ']';

  if ( $line != null )
  $msg .=  ' - [' . $line . ']';

  DebugClass::getDebug()->debug( $msg );
}
